package com.getpebble.android.bluetooth.device;

import android.app.Service;
import com.getpebble.android.R;
import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.bluetooth.radio.PebbleBluetoothAdapter;
import com.getpebble.android.bluetooth.radio.PebbleBluetoothAdapter.AdapterState;
import com.getpebble.android.bluetooth.radio.PebbleBluetoothDevice;
import com.getpebble.android.bluetooth.receiver.AdapterStateReceiver;
import com.getpebble.android.bluetooth.receiver.DiscoveryReceiver;
import com.getpebble.android.common.core.trace.Trace;
import com.sun.mail.imap.protocol.ListInfo;
import com.sun.mail.smtp.SMTPMessage;
import java.util.HashSet;
import java.util.Set;

public abstract class AbsDeviceService extends Service implements IRemoteDeviceCallback {
    private static final String TAG = AbsDeviceService.class.getSimpleName();
    static PebbleBluetoothAdapter sPebbleBluetoothAdapter = null;
    private AdapterStateReceiver mAdapterStateReceiver = null;
    private DiscoveryReceiver mDiscoveryReceiver = null;

    static /* synthetic */ class AnonymousClass3 {
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent = new int[RemoteDeviceEvent.values().length];
        static final /* synthetic */ int[] $SwitchMap$com$getpebble$android$bluetooth$radio$PebbleBluetoothAdapter$AdapterState = new int[AdapterState.values().length];

        static {
            try {
                $SwitchMap$com$getpebble$android$bluetooth$radio$PebbleBluetoothAdapter$AdapterState[AdapterState.ON.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$radio$PebbleBluetoothAdapter$AdapterState[AdapterState.OFF.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$radio$PebbleBluetoothAdapter$AdapterState[AdapterState.TURNING_ON.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$radio$PebbleBluetoothAdapter$AdapterState[AdapterState.TURNING_OFF.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$radio$PebbleBluetoothAdapter$AdapterState[AdapterState.ERROR.ordinal()] = 5;
            } catch (NoSuchFieldError e5) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent[RemoteDeviceEvent.BONDED.ordinal()] = 1;
            } catch (NoSuchFieldError e6) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent[RemoteDeviceEvent.UNBONDED.ordinal()] = 2;
            } catch (NoSuchFieldError e7) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent[RemoteDeviceEvent.CONNECT_RESULT.ordinal()] = 3;
            } catch (NoSuchFieldError e8) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent[RemoteDeviceEvent.DISCONNECTED.ordinal()] = 4;
            } catch (NoSuchFieldError e9) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent[RemoteDeviceEvent.DATA_SEND_SUCCESS.ordinal()] = 5;
            } catch (NoSuchFieldError e10) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent[RemoteDeviceEvent.DATA_SEND_FAILED.ordinal()] = 6;
            } catch (NoSuchFieldError e11) {
            }
            try {
                $SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent[RemoteDeviceEvent.DATA_RECEIVED.ordinal()] = 7;
            } catch (NoSuchFieldError e12) {
            }
        }
    }

    protected abstract void onAdapterDisabled();

    protected abstract void onAdapterEnabled();

    protected abstract void onAdapterError();

    protected abstract void onDeviceFound(RemoteDeviceMetaData remoteDeviceMetaData);

    protected abstract void onDiscoveryStarted();

    protected abstract void onDiscoveryStopped();

    protected abstract void onRemoteDeviceBonded(RemoteDeviceMetaData remoteDeviceMetaData);

    protected abstract void onRemoteDeviceConnectionResult(RemoteDeviceMetaData remoteDeviceMetaData, ConnectionResult connectionResult);

    protected abstract void onRemoteDeviceDataReceived(RemoteDeviceMetaData remoteDeviceMetaData, ProtocolMessage protocolMessage);

    protected abstract void onRemoteDeviceDataSendFailed(RemoteDeviceMetaData remoteDeviceMetaData);

    protected abstract void onRemoteDeviceDataSendSuccess(RemoteDeviceMetaData remoteDeviceMetaData);

    protected abstract void onRemoteDeviceDisconnectSuccess(RemoteDeviceMetaData remoteDeviceMetaData);

    protected abstract void onRemoteDeviceUnbonded(RemoteDeviceMetaData remoteDeviceMetaData);

    public final void onEvent(RemoteDevice remoteDevice, RemoteDeviceEvent event, ProtocolMessage message, ConnectionResult result) {
        switch (AnonymousClass3.$SwitchMap$com$getpebble$android$bluetooth$device$RemoteDeviceEvent[event.ordinal()]) {
            case SMTPMessage.RETURN_FULL /*1*/:
                Trace.info(TAG, "Bonded to " + remoteDevice.getName());
                onRemoteDeviceBonded(remoteDevice.getMetaData(this));
                return;
            case SMTPMessage.RETURN_HDRS /*2*/:
                Trace.info(TAG, "Unbonded from " + remoteDevice.getName());
                onRemoteDeviceUnbonded(remoteDevice.getMetaData(this));
                return;
            case ListInfo.INDETERMINATE /*3*/:
                Trace.info(TAG, "Connecting to " + remoteDevice.getName() + ": result = " + result);
                onRemoteDeviceConnectionResult(remoteDevice.getMetaData(this), result);
                return;
            case SMTPMessage.NOTIFY_DELAY /*4*/:
                Trace.info(TAG, "Disconnected from " + remoteDevice.getName());
                onRemoteDeviceDisconnectSuccess(remoteDevice.getMetaData(this));
                return;
            case R.styleable.WalletFragmentStyle_maskedWalletDetailsHeaderTextAppearance /*5*/:
                Trace.info(TAG, "Data sent to " + remoteDevice.getName());
                onRemoteDeviceDataSendSuccess(remoteDevice.getMetaData(this));
                return;
            case R.styleable.WalletFragmentStyle_maskedWalletDetailsBackground /*6*/:
                Trace.info(TAG, "Data failed to send to " + remoteDevice.getName());
                onRemoteDeviceDataSendFailed(remoteDevice.getMetaData(this));
                return;
            case R.styleable.WalletFragmentStyle_maskedWalletDetailsButtonTextAppearance /*7*/:
                onRemoteDeviceDataReceived(remoteDevice.getMetaData(this), message);
                return;
            default:
                Trace.error(TAG, "Unhandled event!");
                return;
        }
    }

    public static PebbleBluetoothAdapter getPebbleBluetoothAdapter() {
        return sPebbleBluetoothAdapter;
    }

    protected boolean startDiscovery() {
        return sPebbleBluetoothAdapter.startDiscovery();
    }

    protected boolean cancelDiscovery() {
        return sPebbleBluetoothAdapter.cancelDiscovery();
    }

    protected boolean connectToDevice(RemoteDevice device) throws IllegalArgumentException {
        if (device == null) {
            throw new IllegalArgumentException("'device' cannot be null!");
        }
        Trace.info(TAG, "connectToDevice(" + device.getName() + ")");
        return device.connect(getApplicationContext());
    }

    protected boolean disconnectFromDevice(RemoteDevice device) throws IllegalArgumentException {
        if (device == null) {
            throw new IllegalArgumentException("'device' cannot be null!");
        }
        Trace.info(TAG, "disconnectFromDevice(" + device.getName() + ")");
        return device.disconnect();
    }

    protected boolean sendMessage(RemoteDevice device, ProtocolMessage message) throws IllegalArgumentException {
        if (message == null) {
            throw new IllegalArgumentException("'message' cannot be null!");
        } else if (device == null) {
            throw new IllegalArgumentException("'device' cannot be null!");
        } else if (device.getStatus().equals(DeviceStatus.CONNECTED)) {
            device.sendMessage(message);
            return true;
        } else {
            Trace.info(TAG, "sendMessage() device: " + device.getAddress() + " is " + device.getStatus());
            return false;
        }
    }

    protected Set<RemoteDeviceMetaData> getBondedDevices() {
        HashSet<RemoteDeviceMetaData> devices = new HashSet();
        PebbleBluetoothAdapter adapter = getPebbleBluetoothAdapter();
        if (adapter != null) {
            Set<PebbleBluetoothDevice> btDevices = adapter.getBondedDevices();
            if (btDevices != null) {
                for (PebbleBluetoothDevice btDevice : btDevices) {
                    RemoteDeviceMetaData device = new RemoteDeviceMetaData();
                    device.setName(btDevice.getName());
                    device.setMacAddress(btDevice.getAddress());
                    device.setBluetoothClass(btDevice.getBluetoothClass());
                    devices.add(device);
                }
            }
        }
        return devices;
    }

    protected PebbleBluetoothAdapter getBluetoothAdapter() {
        return PebbleBluetoothAdapter.createInstance(this);
    }

    public void onCreate() {
        super.onCreate();
        Trace.debug(TAG, "onCreate()");
        sPebbleBluetoothAdapter = getBluetoothAdapter();
        if (sPebbleBluetoothAdapter.hasDevice()) {
            this.mAdapterStateReceiver = new AdapterStateReceiver(this) {
                public void onAdapterStateChanged(AdapterState state) {
                    switch (AnonymousClass3.$SwitchMap$com$getpebble$android$bluetooth$radio$PebbleBluetoothAdapter$AdapterState[state.ordinal()]) {
                        case SMTPMessage.RETURN_FULL /*1*/:
                            Trace.info(AbsDeviceService.TAG, "Adapter is on");
                            AbsDeviceService.this.onAdapterEnabled();
                            return;
                        case SMTPMessage.RETURN_HDRS /*2*/:
                            Trace.info(AbsDeviceService.TAG, "Adapter is off");
                            AbsDeviceService.this.onAdapterDisabled();
                            return;
                        case ListInfo.INDETERMINATE /*3*/:
                            Trace.info(AbsDeviceService.TAG, "Adapter turning on");
                            return;
                        case SMTPMessage.NOTIFY_DELAY /*4*/:
                            Trace.info(AbsDeviceService.TAG, "Adapter turning off");
                            AbsDeviceService.this.onAdapterDisabled();
                            return;
                        default:
                            Trace.error(AbsDeviceService.TAG, "Adapter error!");
                            AbsDeviceService.this.onAdapterError();
                            return;
                    }
                }
            };
            this.mDiscoveryReceiver = new DiscoveryReceiver(this) {
                public void onDiscoveryStarted() {
                    Trace.debug(AbsDeviceService.TAG, "Discovery started");
                    AbsDeviceService.this.onDiscoveryStarted();
                }

                public void onDeviceFound(RemoteDeviceMetaData remoteDeviceMetaData) {
                    Trace.debug(AbsDeviceService.TAG, "Device found");
                    AbsDeviceService.this.onDeviceFound(remoteDeviceMetaData);
                }

                public void onDiscoveryFinished() {
                    Trace.debug(AbsDeviceService.TAG, "Discovery finished");
                    AbsDeviceService.this.onDiscoveryStopped();
                }
            };
            return;
        }
        Trace.error(TAG, "No bluetooth adapter; fatal");
        onAdapterError();
    }

    public void onDestroy() {
        Trace.debug(TAG, "onDestroy()");
        if (this.mAdapterStateReceiver != null) {
            this.mAdapterStateReceiver.stopReceiving();
            this.mAdapterStateReceiver = null;
        }
        if (this.mDiscoveryReceiver != null) {
            this.mDiscoveryReceiver.stopReceiving();
            this.mDiscoveryReceiver = null;
        }
        sPebbleBluetoothAdapter = null;
        super.onDestroy();
    }
}
